package com.otoe.dilidilivid.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.otoe.dilidilivid.data.entity.ChoseItem;
import com.otoe.dilidilivid.R;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import androidx.recyclerview.widget.RecyclerView;

public class ChoseTagAdapter extends RecyclerView.Adapter<ChoseTagAdapter.VH> {

    public ChoseTagAdapter( List<ChoseItem> datas) {
        this.mDatas = datas;
    }
    public static class VH extends RecyclerView.ViewHolder{
        public TextView tv_tag;
        public Context context;
        public VH(View v) {
            super(v);
            tv_tag = (TextView) v.findViewById(R.id.tv_tag);
            context = v.getContext();
        }
    }

    private List<ChoseItem> mDatas;
    private Set<String> mNameSet;
    private static final int maxTagNum = 3;

    @Override
    public void onBindViewHolder(ChoseTagAdapter.VH holder, int position) {
        int p=position;
        mNameSet=new HashSet<>();
        holder.tv_tag.setText(mDatas.get(p).getName());
        holder.tv_tag.setBackground(holder.context.getDrawable(R.drawable.corner_round_gray));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChose = mDatas.get(p).getChose();
                if (!isChose) {
                    if (mNameSet.size() == maxTagNum) {
                        sendToast(holder.context, "您最多只可选择"+maxTagNum+"个标签", Toast.LENGTH_SHORT);
                        return;
                    }
                    mDatas.get(p).setChose(true);
                    mNameSet.add(mDatas.get(p).getName());
                } else {
                    mDatas.get(p).setChose(false);
                    mNameSet.remove(mDatas.get(p).getName());
                }
                updateStatus(holder.context,holder.tv_tag, isChose, mNameSet);
//                mChoseEvent.setNameSet(mNameSet);
//                EventBus.getDefault().post(mChoseEvent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public ChoseTagAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
//         FragmentHomeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.fragment_home,parent,false);
        View v = View.inflate(parent.getContext(), R.layout.tag_select_item, null);
        return new ChoseTagAdapter.VH(v);
    }

    private void updateStatus(Context mContext, TextView textView, boolean isChose, Set<String> set) {
        if (!isChose) {
            if (set.size() <= maxTagNum) {
                textView.setBackground(mContext.getDrawable(R.drawable.corner_round_pink));
                textView.setTextColor(Color.WHITE);
            }
        } else {
            textView.setBackground(mContext.getDrawable(R.drawable.corner_round_gray));
            textView.setTextColor(Color.parseColor("#4a4a4a"));
        }
    }

    void sendToast(Context mContext,String str, int showTime)
    {
        Toast toast = Toast.makeText(mContext, str, showTime);
        toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL , 0, 0);  //设置显示位置
        TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
        toast.show();
    }


}
